TARGETS := mdriver

LOCKER=/afs/csail/proj/courses/6.172
CC := icpc
CXX := icpc
# You can add -Werr to GCC to force all warnings to turn into errors
CFLAGS := -g -Wall
CXXFLAGS := -g -Wall
LDFLAGS := -lpthread

HEADERS := \
	allocator_interface.h \
	config.h \
	fsecs.h \
	mdriver.h \
	memlib.h \
	validator.h

# Blank line ends list.

# If you add a new file called "filename.c", you should
# add "filename.o \" to this list.
OBJS := \
	memlib.o

MDRIVER_OBJS:= \
	allocator.o \
	bad_allocator.o \
	clock.o \
	fcyc.o \
	fsecs.o \
	ftimer.o \
	libc_allocator.o \
	mdriver.o

BENCHMARKS:= cache-scratch.cpp cache-thrash.cpp larson.cpp linux-scalability.c

# Blank line ends list.

OLDMODE := $(shell cat .buildmode 2> /dev/null)
ifeq ($(DEBUG),1)
CFLAGS := -DDEBUG -O0 $(CFLAGS)
CXXFLAGS := -DDEBUG -O0 $(CXXFLAGS)
ifneq ($(OLDMODE),debug)
$(shell echo debug > .buildmode)
endif
else
CFLAGS := -DNDEBUG -O3 $(CFLAGS)
CXXFLAGS := -DNDEBUG -O3 $(CXXFLAGS)
ifneq ($(OLDMODE),nodebug)
$(shell echo nodebug > .buildmode)
endif
endif

# make all targets specified
all: $(TARGETS)

.PHONY: pintool
pintool:
	$(MAKE) -C pintool

mdriver: $(OBJS) $(MDRIVER_OBJS)
	$(CXX) $(LDFLAGS) $(OBJS) $(MDRIVER_OBJS) -o $@

benchmark: $(OBJS) wrapper.cpp
	for benchmark in $(BENCHMARKS); do \
		name=$${benchmark%.*}; \
		echo $(CXX) $(CFLAGS) -DMYMALLOC $(LDFLAGS) $(OBJS) benchmarks/$$benchmark -o $$name; \
		$(CXX) $(CFLAGS) -DMYMALLOC $(LDFLAGS) $(OBJS) benchmarks/$$benchmark -o $$name; \
		echo $(CXX) $(CFLAGS) -DMYMALLOC -DVALIDATE $(LDFLAGS) benchmarks/$$benchmark $(OBJS) -o $$name-validate; \
		$(CXX) $(CFLAGS) -DMYMALLOC -DVALIDATE $(LDFLAGS) benchmarks/$$benchmark $(OBJS) -o $$name-validate; \
		echo $(CXX) $(CFLAGS) $(LDFLAGS) benchmarks/$$benchmark $(OBJS) -o $$name-libc; \
		$(CXX) $(CFLAGS) $(LDFLAGS) benchmarks/$$benchmark $(OBJS) -o $$name-libc; \
	done

# compile objects

# pattern rule for building objects
%.o: %.cxx %.h $(HEADERS) .buildmode Makefile
	$(CXX) $(CXXFLAGS) -c $< -o $@
%.o: %.c %.h $(HEADERS) .buildmode Makefile
	$(CC) $(CFLAGS) -c $< -o $@


# run each of the targets
run: $(TARGETS)
	for X in $(TARGETS) ; do \
		echo $$X -v ; \
		./$$X -v ; \
		echo ; \
	done

# remove targets and .o files as well as output generated by CQ
clean:
	$(RM) $(TARGETS) $(OBJS) $(MDRIVER_OBJS) *.std* .buildmode
	for benchmark in $(BENCHMARKS); do \
		name=$${benchmark%.*}; \
		$(RM) $$name $$name-libc $$name-validate; \
	done
	$(RM) tmp/*.out
